//
//  HotMovieCell.m
//  Movie
//
//  Created by 路贵斌 on 15/11/4.
//  Copyright © 2015年 huazi. All rights reserved.
//

#import "HotMovieCell.h"


#import "PopoverView.h"

#import "HotMovieCellEntity.h"
#import "LowPriceEntity.h"

static NSDictionary * _priceWaveDic = nil;


@interface HotMovieCell ()

@property (nonatomic, weak) IBOutlet UILabel    *nameLabel;
@property (nonatomic, weak) IBOutlet UILabel    *priceLabel;
@property (nonatomic, weak) IBOutlet UILabel    *messageLabel;
@property (nonatomic, weak) IBOutlet UILabel    *showStatusLabel;
@property (nonatomic, weak) IBOutlet UIView     *informationView1;
@property (nonatomic, weak) IBOutlet UIView     *informationView2;
@property (nonatomic, weak) IBOutlet UIButton   *iconButton;
@property (nonatomic, weak) IBOutlet UIButton   *buyButton;
@property (nonatomic, weak) IBOutlet UIButton   *priceWaveButton;
@property (nonatomic, weak) IBOutlet UIButton   *moviePriceButton;

@property (nonatomic, strong) HotMovieCellEntity*entity;

- (IBAction)buyButtonClicked:(UIButton *)btn;
- (IBAction)playMovie:(UIButton *)btn;
- (IBAction)pushToInfomation:(UIButton *)btn;

@end

@implementation HotMovieCell

- (void)awakeFromNib {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _priceWaveDic = @{
                          @"价格平稳":@"pingwen",
                          @"价格上升":@"shangsheng",
                          @"价格下降":@"xiajiang"
                          };
    });
        
}

- (void)resetCellWithEntity:(HotMovieCellEntity *)entity
{
    self.entity = entity;
    [self.iconButton sd_setImageWithURL:[NSURL URLWithString:entity.moviePicture] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"nailing.JPG"]];
    self.messageLabel.text = entity.movieMessage;
    self.nameLabel.text = entity.movieName;
    self.priceLabel.text = entity.moviePrice;
    [self setLowPriceBtnWithEntity:entity];
    [self setPriceWaveButtonWithEntity:entity];
    [self resetBuyButtonWithEntity:entity];
    [self resetInfomationViewWithEntity:entity];
    
}

- (void)setLowPriceBtnWithEntity:(HotMovieCellEntity *)entity
{
    CGFloat minPrice = entity.moviePrice.floatValue;
    for (LowPriceEntity *thirdEntity in entity.lowPrice) {
        minPrice = MIN(thirdEntity.price, minPrice);
    }
    
    [self.moviePriceButton setAttributedTitle:[self createAttStringWithStr1:@"更低价 " Color1:[UIColor blackColor] Str2:[NSString stringWithFormat:@"￥%.1f",minPrice] Color2:[UIColor colorWithHexString:@"f74c45"] fontSize:9] forState: UIControlStateNormal];;
    
}

- (void)resetBuyButtonWithEntity:(HotMovieCellEntity *)entity
{
    NSString * imageName;
    NSString * title;
    UIColor  * color;
    
    if ([entity.buyStatus isEqualToString: @"1"]) {
        imageName = @"gp_frame";
        title = @"购票";
        color = [UIColor colorWithHexString:@"f74c45"];
    }else{
        imageName = @"ys_frame";
        title = @"预售";
        color = UIColorFromRGB(135, 91, 235);
    }

    [self.buyButton setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [self.buyButton setTitle:title forState:UIControlStateNormal];
    [self.buyButton setTitleColor:color forState:UIControlStateNormal];
    
}

- (void)resetInfomationViewWithEntity:(HotMovieCellEntity *)entity
{
    self.informationView1.hidden = YES;
    self.informationView2.hidden = YES;
    if (!entity.movieInformation ||entity.movieInformation.count == 0)  return ;
    NSArray * infViews = @[self.informationView1,self.informationView2];
    for (int i = 0; i < (entity.movieInformation.count <=2 ?:2); i ++) {
        
        UIView * infView = (UIView *)infViews[i];
        infView.hidden = NO;
        
        //[(UILabel *)[infView viewWithTag:1] setText:entity.movieInformation[i]];
    }
}

- (void)setPriceWaveButtonWithEntity:(HotMovieCellEntity *)entity
{
    [self.priceWaveButton setImage:[UIImage imageNamed:_priceWaveDic[entity.priceWave]] forState:UIControlStateNormal];
    [self.priceWaveButton setTitle:entity.priceWave forState:UIControlStateNormal];
    
}

- (IBAction)buyButtonClicked:(UIButton *)sender
{
    CGPoint point = CGPointMake(CGRectGetMidX(sender.frame), CGRectGetMaxY(sender.frame));
    CGPoint windPoint = [self convertPoint:point toView:[UIApplication sharedApplication].keyWindow];
    NSMutableArray *titles = [NSMutableArray array];
    
    for (LowPriceEntity * entity in self.entity.lowPrice) {

        NSAttributedString * title = [self createAttStringWithStr1:entity.thirdName Color1:[UIColor blackColor] Str2:[NSString stringWithFormat:@" ￥%.1f",entity.price] Color2:[UIColor colorWithHexString:@"f74c45"] fontSize:12];

        [titles addObject:title];
    }
    
    PopoverView *pop = [[PopoverView alloc] initWithPoint:windPoint titles:titles images:nil];
    pop.selectRowAtIndex = ^(NSInteger index){
        NSLog(@"select index:%ld", index);
        
    };
    
    [pop show];

}

- (NSAttributedString *)createAttStringWithStr1:(NSString *)string1 Color1:(UIColor *)color1 Str2:(NSString *)string2 Color2:(UIColor *)color2 fontSize:(CGFloat)size
{

    NSMutableAttributedString  * att1 = [[NSMutableAttributedString alloc]initWithString:string1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:size] ,NSForegroundColorAttributeName :color1}];
    NSAttributedString * att2 = [[NSMutableAttributedString alloc]initWithString:string2 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:size] ,NSForegroundColorAttributeName :color2}];
    [att1 appendAttributedString:att2];

    return att1;
    
}

- (IBAction)playMovie:(UIButton *)btn
{
    
}

- (IBAction)pushToInfomation:(UIButton *)btn
{
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
